<?php
	$cid = $_SESSION['cid'];
	$q = mysql_query("SELECT * FROM pank_interest WHERE cid='$cid' ORDER BY lasttime DESC LIMIT 1");
	$time = time();

	if(mysql_num_rows($q) == 0)
	{
		mysql_query("INSERT INTO pank_interest (lasttime, cid) VALUES ('$time', '$cid')");
	}
	else
	{
		$f = mysql_fetch_array($q);

		$interesttime = 60*60*24;
		$difference = $time-$f['lasttime'];
		$interest = 1.5;

		$howmuch = ceil($difference/$interesttime);

		if($difference >= $interesttime)
		{
			for($i = 1; $i < $howmuch; $i++)
			{
				$aid = $_SESSION['uid'];
				$pank = getPankPollars($aid);
				$newpank = floor($pank+(($pank/100)*$interest));
				mysql_query("UPDATE account SET pank='$newpank' WHERE id='$aid'");
				mysql_query("UPDATE pank_interest SET lasttime='$time' WHERE cid='$cid'");
			}
		}
	}

	function getPankTransfers()
	{
		$cid = $_SESSION['cid'];
		$q = mysql_query("SELECT * FROM pank_log WHERE cid='$cid' AND type='1' OR cid='$cid' AND type='2' ORDER BY senddate DESC LIMIT 5");

		$i = 1;

		echo '<table>';

		echo '<tr><td width="40"><b>#</b></td><td width="150"><b>Transaction Type</b></td><td width="170"><b>Amount</b></td><td><b>Date</b></td></tr>';
		while($f = mysql_fetch_array($q))
		{
			echo '<tr><td>'.$i.'</td><td>'.TransactionType($f['type']).'</td><td>&#x2766;'.$f['amount'].'</td><td>'.$f['senddate'].'</td></tr>';
			$i++;
		}
		echo '</table>';
	}

	if(isset($_GET['transfer']) && $_SERVER['REQUEST_METHOD'] == "POST")
	{
		if(!is_numeric($_POST['amount']))
		{
			alert("Amount must be numeric.");
			redirect("?page=transactions");
		}
		else
		{
			$aid = $_SESSION['uid'];
			$cid = $_SESSION['cid'];

			$name = getNameFromCid($cid);

			$destination = $_POST['destination'];
			$amount = $_POST['amount'];

			$character = $_POST['character'];

			$pollars = getPollars($aid);
			
			$ccid = getCidFromName($character);
			$caid = getAidFromName($character);
			$cpollars = getPollars($caid);

			$pank = getPankPollars($aid);

			if(strtolower($character) == strtolower($_SESSION['char']))
			{
				alert("Can't send yourself money.");
				redirect("?page=transactions");
				$error = 1;
			}

			if($_POST['amount'] < 0)
			{
				alert("You can't request money noob.");
				redirect("?page=transactions");
				$error = 1;
			}

			$aid = $_SESSION['uid'];
			$minimum = 1000*getLevel($aid);


			if($destination == 1)
			{
				if($amount > $pollars)
				{
					alert("You don't have that many pollars on your account.");
					redirect("?page=transactions");
					$error = 1;
				}
				else
				{
					$newPollars = $pollars-$amount;
					if($newPollars < $minimum)
					{
						alert("You must have an minimum of $minimum pollars on your account.");
						redirect("?page=transactions");
						$error = 1;
					}
					else
					{
						$newPank = $pank+$amount;
						$type = 1;
					}
					
				}

				
			}
			else if($destination == 0)
			{
				if($amount > $pank)
				{
					alert("You don't have that many pollars on your pank.");
					redirect("?page=transactions");
					$error = 1;
				}
				else
				{
					$newPank = $pank-$amount;
					$newPollars = $pollars+$amount;
					$type = 2;
				}
			}
			else if($destination == 3)
			{
				if($amount > $pollars)
				{
					alert("You don't have that many pollars on your account.");
					redirect("?page=transactions");
					$error = 1;
				}
				else
				{
					$newPollars = $pollars-$amount;
					if($newPollars < $minimum)
					{
						alert("You must have an minimum of $minimum pollars on your account.");
						redirect("?page=transactions");
						$error = 1;
					}
					else
					{
						$CnewPollars = $cpollars+$amount;
						$type = 8;
					}
				}
			}

			if($error != 1)
			{
				$date = date("Y-m-d H:i:s");
				if($type == 1 || $type == 2)
				{
					if(mysql_query("UPDATE account SET pank='$newPank', pollars='$newPollars' WHERE id='$aid'"))
					{
						logTransaction($type, $amount);
						alert("Succesfull transaction!");
						redirect("?page=transactions");
					}
				}
				else
				{
					if(mysql_query("UPDATE account SET pollars='$newPollars' WHERE id='$aid'"))
					{
						if(mysql_query("UPDATE account SET pollars='$CnewPollars' WHERE id='$caid'"))
						{
							logTransaction(8, $amount, TransactionType(8, $name), $cid, $character, $name);
							logTransaction(9, $amount, TransactionType(9, $character), $ccid, $character, $name);

							alert("Succesfull transaction to $character!");
							redirect("?page=transactions");
						}
					}
				}
			}
			
		}
		
	}

	function getSendTransfer($error = null)
	{
		echo '<table>
			<form method="POST" action="?page=transactions&transfer">
				<tr><td>
					<select name="destination">
						<option value="1">Pank</option>
						<option value="0">Account</option>';
						
						if(isset($_GET['name']))
						{
							echo '<option value="3" selected>User</option>';
						}
						else
						{
							echo '<option value="3">User</option>';
						}
						
				
				echo '</select>
				</td>
				<td style="display:none;" class="hidden"><input type="text" placeholder="name" value="'.$_GET['name'].'" name="character"></td>
				<td><input type="text" placeholder="amount" name="amount"></td><td><input type="submit" value="send" class="bsexy"></td><td>'.$error.'</td></tr>
			</form>
		</table>';
	}

	function getReceived()
	{
		$cid = $_SESSION['cid'];
		$name = getNameFromCid($cid);

		$q = mysql_query("SELECT * FROM pank_log WHERE 
						cid='$cid' AND type='3' OR
						cid='$cid' AND type='4' OR
						cid='$cid' AND type='6' OR
						cid='$cid' AND type='7' OR
						receiver='$name' AND type='8'

						ORDER BY senddate DESC LIMIT 10");

		$i = 1;

		echo '<table>';

		echo '<tr><td width="40"><b>#</b></td><td width="250"><b>Transaction Type</b></td><td width="170"><b>Amount</b></td><td><b>Date</b></td></tr>';
		
		$types = array('3','4','6','7', '8');

		while($f = mysql_fetch_array($q))
		{
			if(in_array($f['type'], $types))
			{
				echo '<tr><td>'.$i.'</td><td>'.$f['name'].'</td><td>&#x2766;'.$f['amount'].'</td><td>'.$f['senddate'].'</td></tr>';
				$i++;
			}
		}
		echo '</table>';
	}

	function getPayments()
	{
		$cid = $_SESSION['cid'];
		$name = getNameFromCid($cid);
		
		$q = mysql_query("SELECT * FROM pank_log WHERE 
							cid='$cid' AND type = '5' OR 
							sender='$name' AND type='9'

							ORDER BY senddate DESC LIMIT 10");

		$i = 1;

		echo '<table>';

		echo '<tr><td width="40"><b>#</b></td><td width="250"><b>Transaction Type</b></td><td width="170"><b>Amount</b></td><td><b>Date</b></td></tr>';
		
		$types = array('5', '9', '10');

		while($f = mysql_fetch_array($q))
		{
			if(in_array($f['type'], $types))
			{
				echo '<tr><td>'.$i.'</td><td>'.$f['name'].'</td><td>&#x2766;'.$f['amount'].'</td><td>'.$f['senddate'].'</td></tr>';
				$i++;
			}
		}
		echo '</table>';
	}

	function getPankTransactions()
	{	
		echo '<h3>Send to:</h3>';

			getSendTransfer();

		echo '<h3>Last 5 transfers:</h3>';

			getPankTransfers();

		echo '<h3>Last 10 Received:</h3>';

			getReceived();

		echo '<h3>Last 10 Payments:</h3>';

			getPayments();

	}
?>